<?php

class MenuController extends Controller{

    public function index(){
        $this->getSmarty();
        if(empty( $_SESSION["template"])){
            $_SESSION["template"]="default";
        }
        $this->smarty->display("admin/menu.tpl");
    }

    public function getSubMenu(){
        $pid = isset($_POST ['id']) ? $_POST ['id'] : '0';

        $dbutil = $this->getDB();

        $sql = "select * from tb_menus where menu_p_id='$pid' ";
        $rss = $dbutil->get_results($sql);

        $result = array ();
        foreach ($rss as $rs){
            $result[] = array (
                id=>$rs->menu_id,
                name=>$rs->menu_name,
                isParent=>true,
                desc=>$rs->menu_desc,
                icon=>$rs->menu_icon,
                m_class=>$rs->menu_class,
                href=>$rs->menu_href
            );
        }
        echo json_encode ($result );

    }
    public function removeMenu(){
        $id =  $_POST ['id'];

        $dbutil = $this->getDB();
        
        $data = array();
        $data[] = $id;
        $res = $dbutil->get_results("select menu_id from tb_menus where menu_p_id=$id");
        foreach ($res as $re){
        	$data[] = $re->menu_id;
        }
        
        //删除关联
        $ids = implode(',',$data);
        $dbutil->query(" delete from   tb_role_menu where menu_id in ($ids) ");
        
        //删除菜单
        $dbutil->query(" delete from tb_menus where menu_id=$id or menu_p_id=$id");

        $result = array('success'=>true);

        echo json_encode($result);
    }

    public function addOrmdf(){
        $menuPId = $_POST ['menuPId'];
        $menuName = $_POST ['menuName'];
        $menuDesc = $_POST ['menuDesc'];
        $menuHref = $_POST ['menuHref'];
        $menuIcon = $_POST ['menuIcon'];
        $menuClass = $_POST ['menuClass'];
        $menuId = $_POST ['menuId'];

        $dbutil = $this->getDB();

        $table = "tb_menus";

        $menu_id = 0;
        if(isset($menuPId)){

            $menuId = 0;

            $res = $dbutil->get_results("select MAX(menu_id) ids from tb_menus ");
            foreach ($res as $re){
                $menuId = 1+$re->ids;
                break;
            }

            $dbutil->query(" insert into tb_menus(menu_id,menu_p_id,menu_name,menu_desc,menu_href,menu_icon,menu_class,menu_create_date)
            		values($menuId,$menuPId,'$menuName','$menuDesc','$menuHref','$menuIcon','$menuClass',sysdate())");
        }else{
     
            $dbutil->query(" update tb_menus set menu_name='$menuName',menu_desc='$menuDesc',menu_href='$menuHref',
            		menu_icon='$menuIcon',menu_class='$menuClass',menu_create_date=sysdate() where menu_id=$menuId");
        }
        $result =array(
            success=>true,
            id=>$menuId,
            pid=>$menuPId,
            name=>$menuName,
            desc=>$menuDesc,
            href=>$menuHref,
            icon=>$menuIcon,
            m_class=>$menuClass
        );
        echo json_encode ($result );
// 		$this->getSmarty();
// 		$this->smarty->display("admin/menu.tpl");

    }
    public function menulist(){

        $role_id = $_POST['role_id'];

        $sql = " SELECT DISTINCT me.menu_id,me.menu_name,me.menu_p_id ";
        if($role_id != null){
            $sql = $sql.",rm.role_id ";
        }
        $sql = $sql." from tb_menus me LEFT JOIN tb_role_menu rm on me.menu_id = rm.menu_id ";
        if($role_id != null){
            $sql = $sql." and rm.role_id = $role_id";
        }


        $dbutil = $this->getDB();
        $rss = $dbutil->get_results($sql);

        $result = array ();
        foreach ($rss as $rs){
            $cc = $rs->role_id;
            if($cc != null && $cc != ""){
                $cc = true;
            }else{
                $cc = false;
            }

            $result[] = array (
                id=>$rs->menu_id,
                name=>$rs->menu_name,
                pId=>$rs->menu_p_id,
                checked=>$cc
            );
        }

        echo json_encode ($result );
    }

}

?>